package com.dcivision.workflow.applet;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.io.ByteArrayInputStream;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.BasicMarqueeHandler;
import org.jgraph.graph.CellMapper;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.Edge;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.jgraph.pad.DefaultGraphModelFileFormatXML;
import org.jgraph.pad.GPGraph;
import org.jgraph.util.JGraphUtilities;

import com.dcivision.workflow.applet.net.HttpOperation;

public class WorkflowTreeApplet extends JApplet{
  String response;
  private Container contentPane ;
  protected GPGraph graph;
  private URL baseUrl;
  private HttpOperation httpOperation = new HttpOperation();
  String urlString;
  String projectName;
  String loginName;
  String loginPsw;
  String locale;
  public void init(){
    try{
      baseUrl=this.getCodeBase();
      contentPane = getContentPane();      
      loginName = getParameter("loginName");
      loginPsw = getParameter("loginPsw");
      loginPsw = Crypt.decrypt(loginPsw, "dcivision");
      locale = getParameter("locale");
      if("1".equals("1")){
        urlString="http://zealot:8080/eip/login.do";
        baseUrl = new URL("http://zealot:8080");
      } else {      
        urlString  = this.getDocumentBase().toString();
        baseUrl = getCodeBase();
      }
      //System.out.println(baseURL);
      projectName = WindowUtilities.splitString(urlString,"/")[3];
      httpOperation.setBaseURL(baseUrl);
      httpOperation.setProjectName(projectName);
      contentPane.setLayout(new BorderLayout());
      graph = new MyGraph(new MyModel());      
      contentPane.add(new JScrollPane(graph), BorderLayout.CENTER);
      if (!httpOperation.login(baseUrl,loginName,loginPsw,"eip")){
        JOptionPane.showInternalMessageDialog(this,"Login failed!","Message",JOptionPane.INFORMATION_MESSAGE);
        this.destroy();
      }
      Object obj = WindowUtilities.getObjectFromServlet(httpOperation,"/workflow/appletMiddleware?dataType=GET_WORKFLOWCHART&workflowRecordID=100");
      String workflowChart = (String)obj;
      ByteArrayInputStream BAIS = new ByteArrayInputStream(workflowChart.getBytes());        
      DefaultGraphModelFileFormatXML.read(BAIS,graph);
      Object[] cells = graph.getRoots();
      AttributeMap map = new AttributeMap();
      Color progressColor = Color.RED;
      GraphConstants.setLineColor(map, progressColor);
      JGraphUtilities.editCells(graph, graph.getSelectionCells(), map);
      graph.clearSelection();
      DefaultEdge edge = new DefaultEdge();
      for(int i = 0 ;i<cells.length ;i++){
        if(cells[i] != null){                
          if (cells[i] instanceof DefaultEdge ) {
            edge = (DefaultEdge)cells[i];
            
          }
        }
      }
    }catch(Exception e){
     e.printStackTrace();
    }
  }
  
  
  //
  // Custom Graph
  //

  // Defines a Graph that uses the Shift-Button (Instead of the Right
  // Mouse Button, which is Default) to add/remove point to/from an edge.
  public class MyGraph extends GPGraph {

public static final String REVISION = "$Revision: 1.4 $";


      // Construct the Graph using the Model as its Data Source
      public MyGraph(GraphModel model) {
          super(model);
          // Use a Custom Marquee Handler
          setMarqueeHandler(new BasicMarqueeHandler());
          // Tell the Graph to Select new Cells upon Insertion
          //setSelectNewCells(true);
          // Make Ports Visible by Default
          setPortsVisible(true);
          // Use the Grid (but don't make it Visible)
          setGridEnabled(true);
          // Set the Grid Size to 10 Pixel
          setGridSize(6);
          // Set the Tolerance to 2 Pixel
          setTolerance(2);
      }

      // Override Superclass Method to Return Custom EdgeView
      protected EdgeView createEdgeView(Edge e, CellMapper cm) {
          // Return Custom EdgeView
          return new EdgeView(e) {
              // Override Superclass Method
              public boolean isAddPointEvent(MouseEvent event) {
                  // Points are Added using Shift-Click
                  return event.isShiftDown();
              }
              // Override Superclass Method
              public boolean isRemovePointEvent(MouseEvent event) {
                  // Points are Removed using Shift-Click
                  return event.isShiftDown();
              }
          };
      }
  }

  //
  // Custom Model
  //

  // A Custom Model that does not allow Self-References
  public class MyModel extends DefaultGraphModel {

public static final String REVISION = "$Revision: 1.4 $";

      // Override Superclass Method
      public boolean acceptsSource(Object edge, Object port) {
          // Source only Valid if not Equal Target
          return (((Edge) edge).getTarget() != port);
      }
      // Override Superclass Method
      public boolean acceptsTarget(Object edge, Object port) {
          // Target only Valid if not Equal Source
          return (((Edge) edge).getSource() != port);
      }
  }

}
